package com.hrzyit.whistle.im.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.hrzyit.base.utils.IMUtils
import com.hrzyit.whistle.im.R
import java.util.*

/**
 * 自定义表情底部指示器
 */
class IndicatorView @JvmOverloads constructor(
    private val mContext: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(mContext, attrs, defStyleAttr) {
    private var mImageViews: ArrayList<View>? = null//所有指示器集合
    private val size = 6
    private val marginSize = 15
    private val pointSize: Int//指示器的大小
    private val marginLeft: Int//间距

    init {
        pointSize = IMUtils.dp2px(mContext, size.toFloat())
        marginLeft = IMUtils.dp2px(mContext, marginSize.toFloat())
    }

    /**
     * 初始化指示器
     *
     * @param count 指示器的数量
     */
    fun initIndicator(count: Int) {
        mImageViews = ArrayList()
        this.removeAllViews()
        var lp: LinearLayout.LayoutParams
        for (i in 0 until count) {
            val v = View(mContext)
            lp = LinearLayout.LayoutParams(pointSize, pointSize)
            if (i != 0)
                lp.leftMargin = marginLeft
            v.layoutParams = lp
            if (i == 0) {
                v.setBackgroundResource(R.drawable.bg_circle_white)
            } else {
                v.setBackgroundResource(R.drawable.bg_circle_gary)
            }
            mImageViews!!.add(v)
            this.addView(v)
        }
    }

    /**
     * 页面移动时切换指示器
     */
    fun playByStartPointToNext(startPosition: Int, nextPosition: Int) {
        var startPosition = startPosition
        var nextPosition = nextPosition
        if (startPosition < 0 || nextPosition < 0 || nextPosition == startPosition) {
            nextPosition = 0
            startPosition = nextPosition
        }
        val viewStrat = mImageViews!![startPosition]
        val viewNext = mImageViews!![nextPosition]
        viewNext.setBackgroundResource(R.drawable.bg_circle_white)
        viewStrat.setBackgroundResource(R.drawable.bg_circle_gary)
    }

}
